package org.example.demo;

import java.util.Queue;

/**
 * @Email: jixiaotian.ch@foxmail.com
 * @Date : 2020/9/23 17:12
 */
/*
生产者
 */
public class Producer implements Runnable {

    private Queue<String> msg;
    private int maxSize;

    public Producer(Queue<String> msg, int maxSize) {
        this.msg = msg;
        this.maxSize = maxSize;
    }

    //生产者
    @Override
    public void run() {
        int i = 0;
        while (true) {
            i++;
            synchronized (msg) {
                while (msg.size() == maxSize) {
                    try {
                        msg.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                msg.add("生产者生产消息：" + i);
                System.out.println("生产者生产消息：" + i);
                msg.notify();
            }
        }
    }


}

